﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PropertyUpdate : System.Web.UI.Page
{
    Property q = new Property();
    DataClassesDataContext dc = new DataClassesDataContext();
    String id = "";
    long idd = 0;
    Property prop = new Property();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Session["isValid"].ToString().Equals("true"))
                Response.Redirect("login.aspx");
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            Response.Redirect("login.aspx");
        }


        id = Request.QueryString["property"];
        try
        {
            idd = long.Parse(id);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            Response.Redirect("errorpage.aspx");
        }
        // Get the property
        q =
            (from p in dc.Properties
             where p.ID == idd
             select p).Single();
        //Update page controls


        if (q.PropertyNomos != null)
            if (!Session["usertype"].Equals("adm"))
                this.DropDownList1.Enabled = false;
        if (q.PropertyDimos != null)
            if (!Session["usertype"].Equals("adm"))
                this.DropDownList2.Enabled = false;

        DropDownList1_Populate();

        if (!IsPostBack)
        {
            if (this.DropDownList1.SelectedValue.Equals("aa"))
                this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
            else
            {
                this.DropDownList2.DataSource = from dimos in dc.Dimois
                                                where dimos.Nomos == this.DropDownList1.SelectedValue
                                                orderby dimos.Onoma
                                                select new { name = dimos.Onoma };
                this.DropDownList2.DataTextField = "name";
                this.DropDownList2.DataValueField = "name";
                this.DropDownList2.DataBind();
                this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
                if (!this.q.PropertyDimos.Equals(""))
                    this.DropDownList2.SelectedValue = this.q.PropertyDimos;
            }

        }

        if (!IsPostBack)
        {
            
            if (q.PropertySurface != null)
            {
                this.TextBox1.Text = q.PropertySurface.ToString();
                if (!Session["usertype"].Equals("adm"))
                    this.TextBox1.ReadOnly = true;
            }
            if (q.PropertyArea != null)
            {
                if (!q.PropertyArea.Equals(""))
                {
                    this.TextBox4.Text = q.PropertyArea;
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox4.ReadOnly = true;
                }
            }
            if (q.OSDENumber != null)
            {
                this.TextBox5.Text = q.OSDENumber.ToString();
                if (!Session["usertype"].Equals("adm"))
                    this.TextBox5.ReadOnly = true;
            }
            if (q.AnadasmosNumber != null)
            {
                this.TextBox6.Text = q.AnadasmosNumber.ToString();
                if (!Session["usertype"].Equals("adm"))
                    this.TextBox6.ReadOnly = true;
            }
            if (q.RegistrationOffice != null)
            {
                if (!q.RegistrationOffice.Equals(""))
                {
                    this.TextBox7.Text = q.RegistrationOffice;
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox7.ReadOnly = true;
                }
            }
            if (q.OwnerName != null)
            {
                if (!q.OwnerName.Equals(""))
                {
                    this.TextBox8.Text = q.OwnerName;
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox8.ReadOnly = true;
                }
            }

            if (q.OwnerAddress != null)
            {
                if (!q.OwnerAddress.Equals(""))
                {
                    this.TextBox9.Text = q.OwnerAddress;
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox9.ReadOnly = true;
                }
            }

            if (q.OwnerTF != null)
            {
                if (!q.OwnerTF.Equals(""))
                {
                    this.TextBox10.Text = q.OwnerTF;
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox10.ReadOnly = true;
                }
            }

            if (q.FirstSeedSeed != null)
            {
                //this.TextBox11.Text = q.FirstSeedSeed;
                this.TextBox11.Text = q.FirstSeedSeed.ToString();

                if (!Session["usertype"].Equals("adm"))
                    this.TextBox11.ReadOnly = true;
            }

            if (q.SecondSeedSeed != null)
            {
                this.TextBox12.Text = q.SecondSeedSeed.ToString();
                if (!Session["usertype"].Equals("adm"))
                    this.TextBox12.ReadOnly = true;
            }

            if (q.Apopsigeoponou != null)
            {
                if (!q.Apopsigeoponou.Equals(""))
                {
                    this.TextBox13.Text = q.Apopsigeoponou;
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox13.ReadOnly = true;
                }
            }
        }

        //Hmerologia me postback i xoris?
        if (!IsPostBack)
        {
            if (q.SignatureDate != null)
            {
                this.Calendar1.SelectedDate = (DateTime)q.SignatureDate;
               // if (!Session["usertype"].Equals("adm"))
                 //   this.Calendar1.ReadOnly = true;
            }

            if (q.ReceivedDate != null)
                this.Calendar2.SelectedDate = (DateTime)q.ReceivedDate;

            if (q.FirstSeedDate != null)
                this.Calendar3.SelectedDate = (DateTime)q.FirstSeedDate;

            if (q.SecondSeedDate != null)
                this.Calendar4.SelectedDate = (DateTime)q.SecondSeedDate;

            if (q.ApopsigeoponouDate != null)
                this.Calendar5.SelectedDate = (DateTime)q.ApopsigeoponouDate;

        }

    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        if ((this.q.SignatureDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
        {
            this.q.SignatureDate = this.Calendar1.SelectedDate;
            //this.dc.SubmitChanges();
        }
        else
            this.Calendar1.SelectedDate = (DateTime)this.q.SignatureDate;
    }
    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        if ((this.q.ReceivedDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
        {
            this.q.ReceivedDate = this.Calendar2.SelectedDate;
            //this.dc.SubmitChanges();
        }
        else
            this.Calendar2.SelectedDate = (DateTime)this.q.ReceivedDate;
    }
/*    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        this.q.PropertySurface = double.Parse(this.TextBox1.Text);
    }
    
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {
        this.q.PropertyArea = this.TextBox4.Text;
    }
    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {
        this.q.OSDENumber = long.Parse(this.TextBox5.Text);
    }
    protected void TextBox6_TextChanged(object sender, EventArgs e)
    {
        this.q.AnadasmosNumber = long.Parse(this.TextBox6.Text);
    }
    protected void TextBox7_TextChanged(object sender, EventArgs e)
    {
        this.q.RegistrationOffice = this.TextBox8.Text;
    }
    protected void TextBox8_TextChanged(object sender, EventArgs e)
    {
        this.q.OwnerName = this.TextBox8.Text;
    }
    protected void TextBox9_TextChanged(object sender, EventArgs e)
    {
        this.q.OwnerAddress = this.TextBox9.Text;
    }
    protected void TextBox10_TextChanged(object sender, EventArgs e)
    {
        this.q.OwnerTF = this.TextBox10.Text;
    }
    protected void TextBox11_TextChanged(object sender, EventArgs e)
    {
        //this.q.FirstSeedSeed = this.TextBox11.Text;
        try
        {
            q.FirstSeedSeed = double.Parse(TextBox11.Text);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            //this.Label5.Text = "Παρακαλώ συμπληρώστε το πεδίο μόνο με αριθμούς";
            return;
        }
    }
    protected void TextBox12_TextChanged(object sender, EventArgs e)
    {
        //this.q.SecondSeedSeed = this.TextBox12.Text;
        try
        {
            q.SecondSeedSeed = double.Parse(TextBox12.Text);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            //this.Label5.Text = "Παρακαλώ συμπληρώστε το πεδίο μόνο με αριθμούς";
            return;
        }
    }*/
    protected void Calendar3_SelectionChanged(object sender, EventArgs e)
    {
        if ((this.q.FirstSeedDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
        {
            this.q.FirstSeedDate = this.Calendar3.SelectedDate;
        //    this.dc.SubmitChanges();
        }
        else
            this.Calendar3.SelectedDate = (DateTime)this.q.FirstSeedDate;
    }
    protected void Calendar4_SelectionChanged(object sender, EventArgs e)
    {
        if ((this.q.SecondSeedDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
        {
            this.q.SecondSeedDate = this.Calendar4.SelectedDate;
        //    this.dc.SubmitChanges();
        }
        else
            this.Calendar4.SelectedDate = (DateTime)this.q.SecondSeedDate;
    }
/*    protected void TextBox13_TextChanged(object sender, EventArgs e)
    {
        this.q.Apopsigeoponou = this.TextBox13.Text;
    }*/
    protected void Calendar5_SelectionChanged(object sender, EventArgs e)
    {
        if ((this.q.ApopsigeoponouDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
        {
            this.q.ApopsigeoponouDate = this.Calendar5.SelectedDate;
         //   this.dc.SubmitChanges();
        }
        else
            this.Calendar5.SelectedDate = (DateTime)this.q.ApopsigeoponouDate; ;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
                GridViewExportUtil.Export("SearchResults.xls", this.GridView1);
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (q.PropertyNomos != null)
        {
            if ((this.q.PropertyNomos.Equals("")) || (Session["usertype"].Equals("adm")))
            {
                if (!this.DropDownList1.SelectedValue.Equals("aa"))
                    this.q.PropertyNomos = this.DropDownList1.SelectedItem.Value;
                DropDownList2_Populate();
                this.Label1.Text = "Παρακαλώ επιλέξτε τον νέο δήμο ";
            }
            else
                this.DropDownList1.SelectedValue = this.q.PropertyNomos;
        }
        if (!this.DropDownList1.SelectedValue.Equals("aa"))
            this.q.PropertyNomos = this.DropDownList1.SelectedValue;
        DropDownList2_Populate();
    }

    protected void DropDownList1_Populate()
    {
        if ((this.DropDownList1.SelectedValue.Equals("aa")) || (this.DropDownList1.SelectedValue.Equals("")))
        {
            this.DropDownList1.DataSource = from nomos in dc.Nomois
                                            orderby nomos.Onoma
                                            select new { Onoma = nomos.Onoma };

            this.DropDownList1.DataTextField = "Onoma";
            this.DropDownList1.DataValueField = "Onoma";
            this.DropDownList1.DataBind();

            if (q.PropertyNomos != null)
            {
                this.DropDownList1.SelectedValue = q.PropertyNomos;
            }
            else
            {
                this.DropDownList1.Items.Insert(0, new ListItem("Επιλογή Νομού", "aa"));
            }
        }
    }


    protected void DropDownList2_Populate()
    {

        if ((this.q.PropertyDimos != null) || (HttpContext.Current.Session["usertype"].Equals("adm")))
        {

            if ((!this.DropDownList1.SelectedValue.Equals("aa")))
            {
                if (!this.DropDownList1.SelectedValue.Equals(""))
                {
                    this.DropDownList2.DataSource = from dimos in dc.Dimois
                                                    where dimos.Nomos == this.DropDownList1.SelectedValue
                                                    orderby dimos.Onoma
                                                    select new { name = dimos.Onoma };
                    this.DropDownList2.DataTextField = "name";
                    this.DropDownList2.DataValueField = "name";
                    this.DropDownList2.DataBind();
                    this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
                }
            }
            else
            {
                this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
            }
        }
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Label1.Text = "";
        if (!(this.DropDownList2.SelectedValue.Equals("bb")) || (Session["usertype"].Equals("adm")))
        {
            this.q.PropertyDimos = this.DropDownList2.SelectedItem.Value;
        }
        else
        {
            this.DropDownList2.SelectedValue = this.q.PropertyDimos;
        }

        if (!this.DropDownList2.SelectedValue.Equals("bb"))
            this.q.PropertyDimos = this.DropDownList2.SelectedValue;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {


        /*if ((this.q.PropertyNomos != null) || (Session["usertype"].Equals("adm")))
            if ((!this.DropDownList1.SelectedValue.Equals("")) || (!this.DropDownList1.SelectedValue.Equals("aa")))
                this.q.PropertyNomos = this.DropDownList1.SelectedValue;

        if ((this.q.PropertyDimos != null) || (Session["usertype"].Equals("adm")))
            if ((!this.DropDownList2.SelectedValue.Equals("")) || (!this.DropDownList2.SelectedValue.Equals("aa")))
                this.q.PropertyDimos = this.DropDownList2.SelectedValue;*/
        if (DropDownList1.SelectedValue.Equals("aa"))
        {
            q.PropertyNomos = null;
        }
        else
            q.PropertyNomos = DropDownList1.SelectedValue;
        if (DropDownList2.SelectedValue.Equals("bb"))
        {
            q.PropertyDimos = null;
        }
        else
            q.PropertyDimos = DropDownList2.SelectedValue;
        if (!this.TextBox1.Text.Equals(""))
        {
            try
            {
                this.q.PropertySurface = double.Parse(this.TextBox1.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Επιφάνεια"" μόνο με αριθμούς";
                return;
            }
        }
        if (!this.TextBox4.Text.Equals(""))
        {
            this.q.PropertyArea = this.TextBox4.Text;
        }
        if (!this.TextBox5.Text.Equals(""))
        {
            try
            {
                this.q.OSDENumber = long.Parse(this.TextBox5.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός ΟΣΔΕ"" μόνο με αριθμούς";
                return;
            }
        }
        if (!TextBox6.Text.Equals(""))
        {
            try
            {
                this.q.AnadasmosNumber = long.Parse(this.TextBox6.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός Αναδασμού"" μόνο με αριθμούς";
                return;
            }
        }
        if (!this.TextBox7.Text.Equals(""))
        {
            this.q.RegistrationOffice = this.TextBox7.Text;
        }
        if (!this.TextBox8.Text.Equals(""))
        {
            this.q.OwnerName = this.TextBox8.Text;
        }
        if (!this.TextBox9.Text.Equals(""))
        {
            this.q.OwnerAddress = this.TextBox9.Text;
        }
        if (!this.TextBox10.Text.Equals(""))
        {
            this.q.OwnerTF = this.TextBox10.Text;
        }
        if (!this.TextBox11.Text.Equals(""))
        {
            //this.q.FirstSeedSeed = this.TextBox11.Text;
            try
            {
                q.FirstSeedSeed = double.Parse(TextBox11.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""1η Σπορά-Σπόρος"" μόνο με αριθμούς";
                return;
            }
        }
        if (!this.TextBox12.Text.Equals(""))
        {
            try
            {
                q.SecondSeedSeed = double.Parse(TextBox12.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""2η Σπορά-Σπόρος"" μόνο με αριθμούς";
                return;
            }
        }
        if (Calendar1.SelectedDate.Year != 1)
        {
            q.SignatureDate = Calendar1.SelectedDate;
        }
        if (Calendar2.SelectedDate.Year != 1)
        {
            q.ReceivedDate = Calendar2.SelectedDate;
        }
        if (Calendar3.SelectedDate.Year != 1)
        {
            q.FirstSeedDate = Calendar3.SelectedDate;
        }
        if (Calendar4.SelectedDate.Year != 1)
        {
            q.SecondSeedDate = Calendar4.SelectedDate;
        }
        if (Calendar5.SelectedDate.Year != 1)
        {
            q.ApopsigeoponouDate = Calendar5.SelectedDate;
        }
        //dc.Properties
        this.dc.SubmitChanges();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        PropertyHistory history = new PropertyHistory();

        if (e.CommandName.Equals("Insert"))
        {
            TextBox YearDate = GridView1.FooterRow.FindControl("year") as TextBox;
            TextBox OfficeSup = GridView1.FooterRow.FindControl("officesupervisor") as TextBox;
            TextBox FieldSup = GridView1.FooterRow.FindControl("fieldsupervisor") as TextBox;
            TextBox agriculturist = GridView1.FooterRow.FindControl("agriculturist") as TextBox;
            TextBox agriculturistapopsi = GridView1.FooterRow.FindControl("agriculturistapopsi") as TextBox;
            TextBox mixanima1 = GridView1.FooterRow.FindControl("mixanimatherismou1") as TextBox;
            TextBox tfmixanima1 = GridView1.FooterRow.FindControl("tfmixanimatos1") as TextBox;
            TextBox mixanima2 = GridView1.FooterRow.FindControl("mixanimatherismou2") as TextBox;
            TextBox tfmixanima2 = GridView1.FooterRow.FindControl("tfmixanimatos2") as TextBox;
            TextBox production = GridView1.FooterRow.FindControl("production") as TextBox;
            TextBox productionkindseed = GridView1.FooterRow.FindControl("productionkindseed") as TextBox;
            TextBox productionquantity = GridView1.FooterRow.FindControl("productionquantity") as TextBox;
            TextBox productionbuyprice = GridView1.FooterRow.FindControl("productionbuyprice") as TextBox;
            Calendar agriculturistApopsidate = GridView1.FooterRow.FindControl("agriculturistApopsidate") as Calendar;
            Calendar HarvestDate = GridView1.FooterRow.FindControl("harvestdate") as Calendar;
            TextBox productionquantitymass = GridView1.FooterRow.FindControl("productionquantitymass") as TextBox;
            TextBox productionbuypricemass = GridView1.FooterRow.FindControl("productionbuypricemass") as TextBox;
            TextBox productionkindmass = GridView1.FooterRow.FindControl("productionkindmass") as TextBox;

            history.PropertyID = idd;
            if (!YearDate.Text.Equals(""))
                history.YearDate = int.Parse(YearDate.Text);
            else
            {
                HttpContext.Current.Session["message"] = "Παρακαλώ εισάγετε το έτος του ιστορικού";
                return;
            }
            if (!OfficeSup.Text.Equals(""))
                history.OfficeSupervisor = OfficeSup.Text;
            if (!FieldSup.Text.Equals(""))
                history.FieldSupervisor = FieldSup.Text;
            if (!agriculturist.Text.Equals(""))
                history.agriculturist = agriculturist.Text;
            if (!agriculturistapopsi.Text.Equals(""))
                history.agriculturistApopsi = agriculturistapopsi.Text;
            if (!mixanima1.Text.Equals(""))
                history.MixanimaTherismou1 = mixanima1.Text;
            if (!tfmixanima1.Text.Equals(""))
                history.TFMixanimatos1 = tfmixanima1.Text;
            if (!mixanima2.Text.Equals(""))
                history.MixanimaTherismou2 = mixanima2.Text;
            if (!tfmixanima2.Text.Equals(""))
                history.TFMixanimatos2 = tfmixanima2.Text;
            if (!production.Text.Equals(""))
                try
                {
                    history.Production = double.Parse(production.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την παραγωγή με αριθμό";
                    return;
                }
            if (!productionkindseed.Text.Equals(""))
                history.ProductionKindSeed = productionkindseed.Text;
            if (!productionkindmass.Text.Equals(""))
                history.ProductionKindMass = productionkindmass.Text;
            if (!productionquantity.Text.Equals(""))
                try
                {
                    history.ProductionQuantitySeed = double.Parse(productionquantity.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                    return;
                }
            if (!productionquantitymass.Text.Equals(""))
                try
                {
                    history.ProductionQuantityMass = double.Parse(productionquantitymass.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                    return;
                }
            if (!productionbuyprice.Text.Equals(""))
                try
                {
                history.ProductionBuyPriceSeed = double.Parse(productionbuyprice.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                    return;
                }
            if (!productionbuypricemass.Text.Equals(""))
                try
                {
                history.ProductionBuyPriceMass = double.Parse(productionbuypricemass.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                    return;
                }
            if (!agriculturistApopsidate.SelectedDate.ToString().Equals(""))
                history.agriculturistApopsiDate = agriculturistApopsidate.SelectedDate;
            if (!HarvestDate.SelectedDate.ToString().Equals(""))
                history.HarvestDate = HarvestDate.SelectedDate;

            dc.PropertyHistories.InsertOnSubmit(history);
            try
            {
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = "Υπάρχει ήδη ιστορικό για αυτό το έτος";
                return;
            }
            GridView1.DataBind();
        }

        if (e.CommandName.Equals("EmptyInsert"))
        {
            TextBox year = GridView1.Controls[0].Controls[0].FindControl("eEtosText") as TextBox;
            TextBox OfficeSup = GridView1.Controls[0].Controls[0].FindControl("eYpeuthinosGrafeiouText") as TextBox;
            TextBox FieldSup = GridView1.Controls[0].Controls[0].FindControl("eYpeuthinosKalText") as TextBox;
            TextBox agriculturist = GridView1.Controls[0].Controls[0].FindControl("eGeoponosText") as TextBox;
            TextBox agriculturistapopsi = GridView1.Controls[0].Controls[0].FindControl("eGeopApopsiText") as TextBox;
            TextBox mixanima1 = GridView1.Controls[0].Controls[0].FindControl("eMixanima1Text") as TextBox;
            TextBox tfmixanima1 = GridView1.Controls[0].Controls[0].FindControl("eMihanima1TFText") as TextBox;
            TextBox mixanima2 = GridView1.Controls[0].Controls[0].FindControl("eMihanima2Text") as TextBox;
            TextBox tfmixanima2 = GridView1.Controls[0].Controls[0].FindControl("eMixanima2TFText") as TextBox;
            TextBox production = GridView1.Controls[0].Controls[0].FindControl("eParagogiText") as TextBox;
            TextBox productionkindseed = GridView1.Controls[0].Controls[0].FindControl("eProdSeedText") as TextBox;
            TextBox productionquantity = GridView1.Controls[0].Controls[0].FindControl("eProdSporosQuantText") as TextBox;
            TextBox productionkindprice = GridView1.Controls[0].Controls[0].FindControl("ePriceSeed") as TextBox;
            Calendar agriculturistApopsidate = GridView1.Controls[0].Controls[0].FindControl("eApopsiGeoDate") as Calendar;
            Calendar HarvestDate = GridView1.Controls[0].Controls[0].FindControl("eTherismosDate") as Calendar;
            TextBox productionkindmass = GridView1.Controls[0].Controls[0].FindControl("eProdMassText") as TextBox;
            TextBox productionquantitymass = GridView1.Controls[0].Controls[0].FindControl("eProdMassQuantText") as TextBox;
            TextBox productionbuypricemass = GridView1.Controls[0].Controls[0].FindControl("eMassPriceText") as TextBox;

            history.PropertyID = idd;
            if (!year.Text.Equals(""))
            {
                try
                {
                    history.YearDate = int.Parse(year.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το έτος";
                    return;
                }
            }
            else
            {
                HttpContext.Current.Session["message"] = "Συμπληρώστε το έτος";
                return;
            }


            if (history.YearDate < 1900)
            {
                HttpContext.Current.Session["message"] = "Συμπληρώστε το έτος";
                return;
            }
            if (!OfficeSup.Text.Equals(""))
                history.OfficeSupervisor = OfficeSup.Text;
            if (!FieldSup.Text.Equals(""))
                history.FieldSupervisor = FieldSup.Text;
            if (!agriculturist.Text.Equals(""))
                history.agriculturist = agriculturist.Text;
            if (!agriculturistapopsi.Text.Equals(""))
                history.agriculturistApopsi = agriculturistapopsi.Text;
            if (!mixanima1.Text.Equals(""))
                history.MixanimaTherismou1 = mixanima1.Text;
            if (!tfmixanima1.Text.Equals(""))
                history.TFMixanimatos1 = tfmixanima1.Text;
            if (!mixanima2.Text.Equals(""))
                history.MixanimaTherismou2 = mixanima2.Text;
            if (!tfmixanima2.Text.Equals(""))
                history.TFMixanimatos2 = tfmixanima2.Text;
            if (!production.Text.Equals(""))
                try
                {
                history.Production = double.Parse(production.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την παραγωγή με αριθμό";
                    return;
                }
            if (!productionkindseed.Text.Equals(""))
                history.ProductionKindSeed = productionkindseed.Text;
            if (!productionquantity.Text.Equals(""))
                try
                {
                    history.ProductionQuantityMass = double.Parse(productionquantity.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                    return;
                }

//            history.ProductionQuantitySeed = double.Parse(productionquantity.Text);
            if (!productionkindprice.Text.Equals(""))
                try
                {
                history.ProductionBuyPriceSeed = double.Parse(productionkindprice.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                    return;
                }
  //          if ((!agriculturistApopsidate.SelectedDate.ToString().Equals("")) && agriculturistApopsidate.SelectedDate.Year!=1)
                history.agriculturistApopsiDate = agriculturistApopsidate.SelectedDate;
    //        if ((!HarvestDate.SelectedDate.ToString().Equals("")) && HarvestDate.SelectedDate.Year != 1)
                history.HarvestDate = HarvestDate.SelectedDate;
            if (!productionkindmass.Text.Equals(""))
                history.ProductionKindMass=productionkindmass.Text;
            if (!productionquantitymass.Text.Equals(""))
                try
                {
                history.ProductionQuantityMass = double.Parse(productionquantitymass.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                    return;
                }
            if (!productionbuypricemass.Text.Equals(""))
                try
                {
                history.ProductionBuyPriceMass = double.Parse(productionbuypricemass.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                    return;
                }

            dc.PropertyHistories.InsertOnSubmit(history);
            dc.SubmitChanges();
            Response.Redirect("propertyupdate.aspx?property=" + id);
        }

    }

    public void agriculturistApopsiDateShow(Object sender, EventArgs e)
    {
        Button b = (Button)this.GridView1.FooterRow.FindControl("agriculturistApopsidateButton");
        b.Visible = false;

        Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("agriculturistApopsidate");
        c.Visible = true;
    }

    public void agriculturistApopsiDateHide(Object sender, EventArgs e)
    {
        Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("agriculturistApopsidate");
        c.Visible = false;

        Button b = (Button)this.GridView1.FooterRow.FindControl("agriculturistApopsidateButton");
        b.Visible = true;
        b.Text = c.SelectedDate.ToShortDateString();
    }

    public void HarvestDateShow(Object sender, EventArgs e)
    {
        Button b = (Button)this.GridView1.FooterRow.FindControl("HarvestDateButton");
        b.Visible = false;

        Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("HarvestDate");
        c.Visible = true;
    }

    public void HarvestDateHide(Object sender, EventArgs e)
    {
        Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("HarvestDate");
        c.Visible = false;

        Button b = (Button)this.GridView1.FooterRow.FindControl("HarvestDateButton");
        b.Visible = true;
        b.Text = c.SelectedDate.ToShortDateString();
    }

}